<template>
  <view class="goals-container">
    <view class="current-status">
      <view class="status-title">当前状态</view>
      <view class="status-grid">
        <view class="status-item">
          <text class="status-value">{{ currentStatus.weight }}kg</text>
          <text class="status-label">当前体重</text>
        </view>
        <view class="status-item">
          <text class="status-value">{{ currentStatus.bmi }}</text>
          <text class="status-label">BMI指数</text>
        </view>
        <view class="status-item">
          <text class="status-value">{{ currentStatus.bodyFat }}%</text>
          <text class="status-label">体脂率</text>
        </view>
      </view>
    </view>

    <view class="goal-section">
      <view class="section-title">
        <uni-icons type="flag" size="20" color="#1890ff" />
        <text>体重目标</text>
      </view>
      <view class="goal-item">
        <text class="goal-label">目标体重</text>
        <view class="goal-input">
          <input 
            v-model="goals.targetWeight" 
            type="digit" 
            placeholder="请输入目标体重"
          />
          <text class="unit">kg</text>
        </view>
      </view>
      <view class="goal-item">
        <text class="goal-label">目标时间</text>
        <picker mode="date" :value="goals.targetDate" @change="updateTargetDate">
          <view class="date-picker">{{ goals.targetDate }}</view>
        </picker>
      </view>
      <view class="progress-bar">
        <view class="progress-info">
          <text>进度: {{ weightProgress }}%</text>
          <text>还需减重 {{ remainingWeight }}kg</text>
        </view>
        <view class="progress-track">
          <view class="progress-fill" :style="{ width: weightProgress + '%' }"></view>
        </view>
      </view>
    </view>

    <view class="nutrition-goals">
      <view class="section-title">
        <uni-icons type="heart" size="20" color="#52c41a" />
        <text>营养目标</text>
      </view>
      <view class="nutrition-item" v-for="nutrition in nutritionGoals" :key="nutrition.type">
        <view class="nutrition-header">
          <text class="nutrition-name">{{ nutrition.name }}</text>
          <text class="nutrition-current">{{ nutrition.current }}/{{ nutrition.target }}{{ nutrition.unit }}</text>
        </view>
        <view class="nutrition-progress">
          <view class="nutrition-track">
            <view 
              class="nutrition-fill" 
              :style="{ 
                width: (nutrition.current / nutrition.target * 100) + '%',
                backgroundColor: nutrition.color 
              }"
            ></view>
          </view>
          <text class="nutrition-percent">{{ Math.round(nutrition.current / nutrition.target * 100) }}%</text>
        </view>
      </view>
    </view>

    <view class="weekly-plan">
      <view class="section-title">
        <uni-icons type="calendar" size="20" color="#ff9500" />
        <text>本周计划</text>
      </view>
      <view class="plan-list">
        <view class="plan-item" v-for="plan in weeklyPlans" :key="plan.id">
          <view class="plan-day">{{ plan.day }}</view>
          <view class="plan-content">
            <text class="plan-title">{{ plan.title }}</text>
            <text class="plan-desc">{{ plan.description }}</text>
          </view>
          <view class="plan-status">
            <uni-icons 
              :type="plan.completed ? 'checkmarkempty' : 'circle'" 
              :color="plan.completed ? '#52c41a' : '#ccc'"
              size="20"
            />
          </view>
        </view>
      </view>
    </view>

    <button class="save-btn" @click="saveGoals">保存目标</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      currentStatus: {
        weight: 65.5,
        bmi: 22.8,
        bodyFat: 18.5
      },
      goals: {
        targetWeight: '60',
        targetDate: '2024-06-15'
      },
      nutritionGoals: [
        {
          type: 'calories',
          name: '每日热量',
          current: 1580,
          target: 1800,
          unit: '千卡',
          color: '#1890ff'
        },
        {
          type: 'protein',
          name: '蛋白质',
          current: 65,
          target: 80,
          unit: 'g',
          color: '#52c41a'
        },
        {
          type: 'fiber',
          name: '膳食纤维',
          current: 18,
          target: 25,
          unit: 'g',
          color: '#ff9500'
        },
        {
          type: 'water',
          name: '饮水量',
          current: 1.5,
          target: 2.0,
          unit: 'L',
          color: '#1890ff'
        }
      ],
      weeklyPlans: [
        {
          id: 1,
          day: '周一',
          title: '低卡饮食',
          description: '控制热量摄入在1600千卡以内',
          completed: true
        },
        {
          id: 2,
          day: '周二',
          title: '高蛋白餐',
          description: '增加蛋白质摄入，目标80g',
          completed: true
        },
        {
          id: 3,
          day: '周三',
          title: '均衡营养',
          description: '保持营养均衡，多吃蔬菜',
          completed: false
        },
        {
          id: 4,
          day: '周四',
          title: '轻食日',
          description: '以轻食为主，减少油腻食物',
          completed: false
        }
      ]
    }
  },
  computed: {
    weightProgress() {
      const current = this.currentStatus.weight
      const target = parseFloat(this.goals.targetWeight)
      const initial = 70 // 假设初始体重
      return Math.round((initial - current) / (initial - target) * 100)
    },
    
    remainingWeight() {
      return Math.max(0, this.currentStatus.weight - parseFloat(this.goals.targetWeight)).toFixed(1)
    }
  },
  methods: {
    updateTargetDate(event) {
      this.goals.targetDate = event.detail.value
    },
    
    saveGoals() {
      uni.showToast({
        title: '保存成功',
        icon: 'success'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.goals-container {
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.current-status {
  background: white;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
}

.status-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
}

.status-grid {
  display: flex;
  justify-content: space-between;
}

.status-item {
  text-align: center;
  flex: 1;
}

.status-value {
  display: block;
  font-size: 36rpx;
  font-weight: bold;
  color: #1890ff;
  margin-bottom: 10rpx;
}

.status-label {
  font-size: 24rpx;
  color: #666;
}

.goal-section, .nutrition-goals, .weekly-plan {
  background: white;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
}

.section-title {
  display: flex;
  align-items: center;
  font-size: 30rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
  gap: 10rpx;
}

.goal-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
  
  &:last-child {
    border-bottom: none;
  }
}

.goal-label {
  font-size: 28rpx;
  color: #333;
}

.goal-input {
  display: flex;
  align-items: center;
  gap: 10rpx;
}

.goal-input input {
  text-align: right;
  color: #1890ff;
  font-size: 28rpx;
}

.unit {
  font-size: 24rpx;
  color: #666;
}

.date-picker {
  color: #1890ff;
  font-size: 28rpx;
}

.progress-bar {
  margin-top: 20rpx;
}

.progress-info {
  display: flex;
  justify-content: space-between;
  font-size: 24rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.progress-track {
  height: 8rpx;
  background: #f0f0f0;
  border-radius: 4rpx;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: #52c41a;
  border-radius: 4rpx;
  transition: width 0.3s;
}

.nutrition-item {
  margin-bottom: 25rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
}

.nutrition-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10rpx;
}

.nutrition-name {
  font-size: 26rpx;
  color: #333;
}

.nutrition-current {
  font-size: 24rpx;
  color: #666;
}

.nutrition-progress {
  display: flex;
  align-items: center;
  gap: 15rpx;
}

.nutrition-track {
  flex: 1;
  height: 6rpx;
  background: #f0f0f0;
  border-radius: 3rpx;
  overflow: hidden;
}

.nutrition-fill {
  height: 100%;
  border-radius: 3rpx;
  transition: width 0.3s;
}

.nutrition-percent {
  font-size: 22rpx;
  color: #666;
  min-width: 60rpx;
}

.plan-item {
  display: flex;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
  
  &:last-child {
    border-bottom: none;
  }
}

.plan-day {
  width: 80rpx;
  font-size: 24rpx;
  color: #666;
}

.plan-content {
  flex: 1;
  margin-left: 20rpx;
}

.plan-title {
  font-size: 26rpx;
  color: #333;
  display: block;
  margin-bottom: 5rpx;
}

.plan-desc {
  font-size: 22rpx;
  color: #666;
}

.save-btn {
  width: 100%;
  height: 80rpx;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 40rpx;
  font-size: 32rpx;
  margin-top: 20rpx;
}
</style>